//到api/index.js里面对外暴露的接口
import { getBannerList, getFloorList } from '@/api';
// 存储状态的仓库
const state = {
    cateGoryList: JSON.parse(localStorage.getItem("cateGoryList")) || [],
    bannerList: [],
    floorList: []
}
// 修改state中的唯一手段
const mutations = {
    setCategoryList(state, cateGoryList) {
        //本地缓存商品分类数据   和sessionStorage不一样的，sessionStorage关闭页面数据会丢失，			localStorage不会
        //localStorage.setItem('cateGoryList', JSON.stringify(cateGoryList));
        state.cateGoryList = cateGoryList; //最后将cateGoryList 存入到vuex中store对象中
    },
    GETBANNERLIST(state, bannerList) {
        //将轮播图的数据保存到vuex里面state仓库里面
        state.bannerList = bannerList
    },
    GETFLOORLIST(state, floorList) {
        //将轮播图的数据保存到vuex里面state仓库里面
        state.floorList = floorList
    }

}
// 处理action 可以处理自己的业务逻辑，可以处理异步请求
const actions = {
    //{commit} 是一个context上下文  通过commmit将数据提交给mutations里面
    //getBannerInfos
    async getBannerList({ commit }) {
        //调用/mock/banner请求获取banner.json里面数据
        let bannerList = await getBannerList();
        console.log(bannerList.data.data)
        if (bannerList.data.code === 200) {
            //通过commit将bannerList.data
            //提交到mutations.GETBANNERLIST
            commit("GETBANNERLIST", bannerList.data.data)
        }
    },
    async getFloorList({ commit }) {
        //调用/mock/banner请求获取banner.json里面数据
        let floor = await getFloorList();
        debugger
        console.log(floor.data.data)
        if (floor.data.code === 200) {
            //通过commit将floor.data
            //提交到mutations.GETBANNERLIST
            commit("GETFLOORLIST", floor.data.data)
        }
    },
}

const getters = {}
//对外暴露出这四个对象
export default {
    state, mutations, actions, getters
}